package com.dmgis.qxfw.dataservice.businessservice.yyhb.service.Impl;


import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.*;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.airpol_live_qx;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.CloudSearch;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.MsgMediumSmallScale;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.RadarSearch;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.Doppler;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.DopplerKey;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.SatelliteNephoramKey;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.AirPolLiveQX;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.ImportantWeather;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.SatelliteNephoram;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.yyhb.service.MeteInfoServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MeteInfoServiceImpl implements MeteInfoServiceI {

	ProcessRequestController procon=new ProcessRequestController();
	@Override
	public DopplerKey getReaderNewDate() {
		// TODO 获取雷达图最新时间
		DopplerKey result = new DopplerKey();
		doppler newestDate = new doppler();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar,'yyyy-MM-dd HH24:MI:SS') as dateChar", "timechar" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc", "timechar desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "doppler");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			newestDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<doppler>>() {
			}).get(0);
		} catch (Exception e) {
			newestDate.setDatechar(DateTime.now().toDate());
		}
		if (newestDate != null) {
			result.setDateChar(newestDate.getDatechar());
			result.setTimeChar(newestDate.getTimechar());
		}
		return result;
	}

	@Override
	public List<DopplerKey> getRadarTypes() {
		// TODO 获取雷达图参数
		List<doppler> listData = new ArrayList<doppler>();
		List<DopplerKey> resultList = new ArrayList<DopplerKey>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "distinct ldtype", "elevation" };
		selectRequest.setColumns(columns);
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(String.format(" and ldtype='%s'", 37));
		strWhere.append(String.format(" or ldtype='%s'", 38));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "doppler");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<doppler>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (doppler dop : listData) {
				DopplerKey dopplerKey = new DopplerKey();
				dopplerKey.setElevation(dop.getElevation());
				dopplerKey.setStationId(dop.getStationid());
				dopplerKey.setType(dop.getLdtype());
				resultList.add(dopplerKey);
			}
		}
		return resultList;
	}

	@Override
	public List<Doppler> getRadar(RadarSearch radarSearch) {
		// TODO 获取雷达图
		List<doppler> listData = new ArrayList<doppler>();
		List<Doppler> resultList = new ArrayList<Doppler>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("TimeChar");
		columns.add("StationId");
		columns.add("LdType");
		columns.add("Elevation");
		columns.add("Path");
		columns.add("jwImgPath");
		columns.add("RadTitle");
		columns.add("fwest");
		columns.add("fsouth");
		columns.add("feast");
		columns.add("fnorth");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "datechar desc", "timechar desc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (radarSearch.getDataYear() != null) {
			strWhere.append(String.format("dataYear='%d'", radarSearch.getDataYear()));
			whereCount++;
		}
		if (radarSearch.getDateChar() != null) {
			strWhere.append(String.format(" and dateChar='%tF'", radarSearch.getDateChar()));
		}
		if (radarSearch.getStartDate() != null) {
			strWhere.append(String.format(
					" and (cast(datechar as timestamp)+ CAST(SUBSTRING(TimeChar,1,2)||'hour' as INTERVAL))>='%s'",
					radarSearch.getStartDate()));
		}
		if (radarSearch.getEndDate() != null) {
			strWhere.append(String.format(
					" and (cast(datechar as timestamp)+ CAST(SUBSTRING(TimeChar,1,2)||'hour' as INTERVAL))<='%s'",
					radarSearch.getEndDate()));
		}
		if (!StringUtil.IsNullOrEmpty(radarSearch.getTimeChar())) {
			strWhere.append(String.format(" and timeChar='%s'", radarSearch.getTimeChar()));
		}
		if (!StringUtil.IsNullOrEmpty(radarSearch.getStationId())) {
			strWhere.append(String.format(" and stationId='%s'", radarSearch.getStationId()));
		}
		if (!StringUtil.IsNullOrEmpty(radarSearch.getType())) {
			strWhere.append(String.format(" and ldtype='%s'", radarSearch.getType()));
		}
		if (radarSearch.getElevation() != null) {
			strWhere.append(String.format(" and elevation='%f'", radarSearch.getElevation()));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "doppler");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<doppler>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (doppler dop : listData) {
				Doppler doppler = new Doppler();
				doppler.setDateChar(dop.getDatechar());
				doppler.setTimeChar(dop.getTimechar());
				doppler.setStationId(dop.getStationid());
				doppler.setType(dop.getLdtype());
				doppler.setElevation(dop.getElevation());
				doppler.setPath(dop.getPath());
				doppler.setJwImgPath(dop.getJwimgpath());
				doppler.setRadTitle(dop.getRadtitle());
				doppler.setFxmin(dop.getFwest());
				doppler.setFxmax(dop.getFeast());
				doppler.setFymin(dop.getFsouth());
				doppler.setFymax(dop.getFnorth());
				resultList.add(doppler);
			}
		}
		return resultList;
	}

	@Override
	public SatelliteNephoramKey getCloudNewDate() {
		// TODO 获取云图最新时间
		SatelliteNephoramKey result = new SatelliteNephoramKey();
		satellitenephoram newestDate = new satellitenephoram();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar,'yyyy-MM-dd HH24:MI:SS') as dateChar", "timechar" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc", "timechar desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "satellitenephoram");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			newestDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<satellitenephoram>>() {
			}).get(0);
		} catch (Exception e) {
			newestDate.setDatechar(DateTime.now().toDate());
		}
		if (newestDate != null) {
			result.setDateChar(newestDate.getDatechar());
			result.setTimeChar(newestDate.getTimechar());
		}
		return result;
	}

	@Override
	public List<SatelliteNephoramKey> getCloudTypes() {
		// TODO 获取云图参数
		List<satellitenephoram> listData = new ArrayList<satellitenephoram>();
		List<SatelliteNephoramKey> resultList = new ArrayList<SatelliteNephoramKey>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "distinct yttype" };
		selectRequest.setColumns(columns);
		try {
			selectParam.put("method", "satellitenephoram");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<satellitenephoram>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (satellitenephoram satel : listData) {
				SatelliteNephoramKey satellite = new SatelliteNephoramKey();
				satellite.setType(satel.getYttype());
				resultList.add(satellite);
			}
		}
		return resultList;
	}

	@Override
	public List<SatelliteNephoram> getCloud(CloudSearch cloudSearch) {
		// TODO 获取云图
		List<satellitenephoram> listData = new ArrayList<satellitenephoram>();
		List<SatelliteNephoram> resultList = new ArrayList<SatelliteNephoram>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("TimeChar");
		columns.add("ytType");
		columns.add("Path");
		columns.add("jwImgPath");
		columns.add("SateTitle");
		columns.add("fwest");
		columns.add("fsouth");
		columns.add("feast");
		columns.add("fnorth");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "datechar desc", "timechar desc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (cloudSearch.getDataYear() != null) {
			strWhere.append(String.format("dataYear='%d'", cloudSearch.getDataYear()));
			whereCount++;
		}
		if (cloudSearch.getDateChar() != null) {
			strWhere.append(String.format(" and dateChar='%tF'", cloudSearch.getDateChar()));
		}
		if (cloudSearch.getStartDate() != null) {
			strWhere.append(String.format(" and datechar>='%s'", cloudSearch.getStartDate()));
		}
		if (cloudSearch.getEndDate() != null) {
			strWhere.append(String.format(" and datechar<='%s'", cloudSearch.getEndDate()));
		}
		if (!StringUtil.IsNullOrEmpty(cloudSearch.getTimeChar())) {
			strWhere.append(String.format(" and timeChar='%s'", cloudSearch.getTimeChar()));
		}
		if (!StringUtil.IsNullOrEmpty(cloudSearch.getType())) {
			strWhere.append(String.format(" and yttype='%s'", cloudSearch.getType()));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "satellitenephoram");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<satellitenephoram>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (satellitenephoram sate : listData) {
				SatelliteNephoram satellite = new SatelliteNephoram();
				satellite.setDateChar(sate.getDatechar());
				satellite.setTimechar(sate.getTimechar());
				satellite.setType(sate.getYttype());
				satellite.setPath(sate.getPath());
				satellite.setJwImgPath(sate.getJwimgpath());
				satellite.setSateTitle(sate.getSatetitle());
				satellite.setFxmin(sate.getFwest());
				satellite.setFxmax(sate.getFeast());
				satellite.setFymin(sate.getFsouth());
				satellite.setFymax(sate.getFnorth());
				resultList.add(satellite);
			}
		}
		return resultList;
	}

	@Override
	public List<smallscale_station> getMetStation(String stations) {
		// TODO Auto-generated method stub
		List<smallscale_station> listData = new ArrayList<smallscale_station>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("stationname");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		strWhere.append(String.format(" stationname in %s", stations));
		strWhere.append(String.format(" and city like '%s%%'", DbConfig.CITY));
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "smallscale_station");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<smallscale_station>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listData;
	}

	@Override
	public List<msgmediumsmallscale> getMsgMediumSmallScale(String startTime, String endTime, String hourOrDay,
																String stations) {
		// TODO Auto-generated method stub
		List<msgmediumsmallscale> resultList = new ArrayList<msgmediumsmallscale>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("StationID");
		columns.add("Longitude");
		columns.add("Latitude");
		columns.add("cloud");
		columns.add("observtime");
		columns.add("sum(cast(rain as float)) as rain");
		columns.add("cast(avg(cast(cast(DryBulbTemp AS VARCHAR) as numeric(18,1))) as float) as drybulbtemp");
		columns.add("max(cast(cast(MaxTemp AS VARCHAR) as float)) as maxtemp");
		columns.add("min(cast(cast(MinTemp AS VARCHAR) as float)) as mintemp");
		columns.add("cast(avg(cast(cast(InstantWindV AS VARCHAR) as numeric(18,1))) as float) as instantwindv");
		columns.add("cast(avg(cast(cast(RelHumidity AS VARCHAR) as numeric(18,1))) as float) as relhumidity");
		columns.add("StationName");
		columns.add("City");
		columns.add("County");
		columns.add("visibility");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (!StringUtil.IsNullOrEmpty(startTime)) {
			strWhere.append(String.format(" and observTime>='%s'", startTime));
		}
		if (!StringUtil.IsNullOrEmpty(endTime)) {
			strWhere.append(String.format(" and observTime<='%s'", endTime));
		}
		/*if (!StringUtil.IsNullOrEmpty(stations)) {
			strWhere.append(String.format(" and stationname in %s", stations));
		}*/
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		strWhere.append(String.format(" and county like '%s%%'", DbConfig.CITY));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[] { "observtime asc" });
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return resultList;
	}

	@Override
	public List<MsgMediumSmallScale> getMediumSmallScale(String startTime, String endTime, String hourOrDay,
														 String stations) {
		// TODO Auto-generated method stub
		List<MsgMediumSmallScale> resultList = new ArrayList<MsgMediumSmallScale>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("StationID");
		columns.add("Longitude");
		columns.add("Latitude");
		columns.add("cloud");
		columns.add("to_char(observtime,'yyyy-MM-dd') as datechar");
		columns.add("sum(cast(rain as float)) as rain");
		columns.add("cast(avg(cast(cast(DryBulbTemp AS VARCHAR) as numeric(18,1))) as float) as drybulbtemp");
		columns.add("max(cast(cast(DryBulbTemp AS VARCHAR) as float)) as maxtemp");
		columns.add("min(cast(cast(DryBulbTemp AS VARCHAR) as float)) as mintemp");
		columns.add("cast(avg(cast(cast(InstantWindV AS VARCHAR) as numeric(18,1))) as float) as instantwindv");
		columns.add("cast(avg(cast(cast(RelHumidity AS VARCHAR) as numeric(18,1))) as float) as relhumidity");
		columns.add("StationName");
		columns.add("City");
		columns.add("County");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (!StringUtil.IsNullOrEmpty(startTime)) {
			strWhere.append(String.format(" and observTime>='%s'", startTime));
		}
		if (!StringUtil.IsNullOrEmpty(endTime)) {
			strWhere.append(String.format(" and observTime<='%s'", endTime));
		}
		if (!StringUtil.IsNullOrEmpty(stations)) {
			strWhere.append(String.format(" and stationname in %s", stations));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[] { "datechar asc" });
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<MsgMediumSmallScale>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return resultList;
	}

	@Override
	public List<ImportantWeather> getHazeVisibility(String s0, String s1) {
		List<important_weather> listData = new ArrayList<important_weather>();
		List<ImportantWeather> resultList = new ArrayList<ImportantWeather>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observetime, 'yyyy-MM-dd HH24:MI:SS') as observetime");
		columns.add("stationid");
		columns.add("stationname");
		columns.add("weathertype");
		columns.add("weatherdetail");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "observetime desc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (s0 != null) {
			strWhere.append(String.format("and observetime>='%s'", s0));
		}
		if (s1 != null) {
			strWhere.append(String.format("and observetime<='%s'", s1));
		}
		strWhere.append(String.format("and weathertype='%s'", "雾霾"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "important_weather");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<important_weather>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (important_weather weather : listData) {
				ImportantWeather importantweather = new ImportantWeather();
				importantweather.setStationId(weather.getStationid());
				importantweather.setStationName(weather.getStationname());
				importantweather.setWeatherType(weather.getWeathertype());
				importantweather.setWeatherInfo(weather.getWeatherdetail());
				importantweather.setTime(weather.getObservetime());
				resultList.add(importantweather);
			}
		}
		return resultList;
	}

	@Override
	public List<AirPolLiveQX> getDailyWeather(String s0, String s1, String s2, String s3, boolean b) {
		List<airpol_live_qx> listData = new ArrayList<airpol_live_qx>();
		List<AirPolLiveQX> resultList = new ArrayList<AirPolLiveQX>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
		columns.add("stationid");
		columns.add("stationname");
		if (s0.contains("WindS")) {
			columns.add(
					"cast(avg(cast(replace(cast(winds AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as winds");
		}
		if (s0.contains("RelHumid")) {
			columns.add(
					"cast(avg(cast(replace(cast(relhumid AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as relhumid");
		}
		if (s0.contains("Rain")) {
			columns.add(
					"cast(avg(cast(replace(cast(rain AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as rain");
		}
		if (s0.contains("Pressure")) {
			columns.add(
					"cast(avg(cast(replace(cast(pressure AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as pressure");
		}
		if (s0.contains("Cloud")) {
			columns.add(
					"cast(avg(cast(replace(cast(cloud AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as cloud");
		}
		if (s0.contains("Visibility")) {
			columns.add(
					"cast(avg(cast(replace(cast(visibility AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as visibility");
		}
		if (s0.contains("MaxTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(maxtemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as maxtemp");
		}
		if (s0.contains("MinTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(mintemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as mintemp");
		}
		if (s0.contains("DryBulbTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(drybulbtemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as drybulbtemp");
		}
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "observtime asc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (b) {
			if (s1 != null) {
				strWhere.append(String.format("and observtime>='%s'", s1));
			}
			if (s2 != null) {
				strWhere.append(String.format("and observtime<='%s'", s2));
			}
		} else {
			if (s1 != null) {
				strWhere.append(String.format("and observtime='%s'", s1));
			}
		}
		strWhere.append(String.format("and stationname='%s'", s3));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "airpol_live_qx");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_live_qx>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (airpol_live_qx airpol : listData) {
				AirPolLiveQX AirPol = new AirPolLiveQX();
				AirPol.setObservTime(airpol.getObservtime());
				AirPol.setStationID(airpol.getStationid());
				AirPol.setStationName(airpol.getStationname());
				if (s0.contains("WindS")) {
					AirPol.setWindS(airpol.getWinds());
				}
				if (s0.contains("RelHumid")) {
					AirPol.setRelHumid(airpol.getRelhumid());
				}
				if (s0.contains("Rain")) {
					AirPol.setRain(airpol.getRain());
				}
				if (s0.contains("Pressure")) {
					AirPol.setPressure(airpol.getPressure());
				}
				if (s0.contains("Cloud")) {
					AirPol.setCloud(airpol.getCloud());
				}
				if (s0.contains("Visibility")) {
					AirPol.setVisibility(airpol.getVisibility());
				}
				if (s0.contains("MaxTemp")) {
					AirPol.setMaxTemp(airpol.getMaxtemp());
				}
				if (s0.contains("MinTemp")) {
					AirPol.setMinTemp(airpol.getMintemp());
				}
				if (s0.contains("DryBulbTemp")) {
					AirPol.setDryBulbTemp(airpol.getDrybulbtemp());
				}
				resultList.add(AirPol);
			}
		}
		return resultList;
	}

	public List<AirPolLiveQX> getDailyWeather1(String s0, String s1, String s2, boolean b) {
		List<airpol_live_qx> listData = new ArrayList<airpol_live_qx>();
		List<AirPolLiveQX> resultList = new ArrayList<AirPolLiveQX>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
		columns.add("stationid");
		columns.add("stationname");
		if (s0.contains("WindS")) {
			columns.add(
					"cast(avg(cast(replace(cast(winds AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as winds");
		}
		if (s0.contains("RelHumid")) {
			columns.add(
					"cast(avg(cast(replace(cast(relhumid AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as relhumid");
		}
		if (s0.contains("Rain")) {
			columns.add(
					"cast(avg(cast(replace(cast(rain AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as rain");
		}
		if (s0.contains("Pressure")) {
			columns.add(
					"cast(avg(cast(replace(cast(pressure AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as pressure");
		}
		if (s0.contains("Cloud")) {
			columns.add(
					"cast(avg(cast(replace(cast(cloud AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as cloud");
		}
		if (s0.contains("Visibility")) {
			columns.add(
					"cast(avg(cast(replace(cast(visibility AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as visibility");
		}
		if (s0.contains("MaxTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(maxtemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as maxtemp");
		}
		if (s0.contains("MinTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(mintemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as mintemp");
		}
		if (s0.contains("DryBulbTemp")) {
			columns.add(
					"cast(avg(cast(replace(cast(drybulbtemp AS VARCHAR),'-65535','0') as numeric(18,1))) as numeric(18,1)) as drybulbtemp");
		}
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "observtime asc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (b) {
			if (s1 != null) {
				strWhere.append(String.format("and observtime>='%s'", s1));
			}
			if (s2 != null) {
				strWhere.append(String.format("and observtime<='%s'", s2));
			}
		} else {
			if (s1 != null) {
				strWhere.append(String.format("and observtime='%s'", s1));
			}
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "airpol_live_qx");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_live_qx>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (airpol_live_qx airpol : listData) {
				AirPolLiveQX AirPol = new AirPolLiveQX();
				AirPol.setObservTime(airpol.getObservtime());
				AirPol.setStationID(airpol.getStationid());
				AirPol.setStationName(airpol.getStationname());
				if (s0.contains("WindS")) {
					AirPol.setWindS(airpol.getWinds());
				}
				if (s0.contains("RelHumid")) {
					AirPol.setRelHumid(airpol.getRelhumid());
				}
				if (s0.contains("Rain")) {
					AirPol.setRain(airpol.getRain());
				}
				if (s0.contains("Pressure")) {
					AirPol.setPressure(airpol.getPressure());
				}
				if (s0.contains("Cloud")) {
					AirPol.setCloud(airpol.getCloud());
				}
				if (s0.contains("Visibility")) {
					AirPol.setVisibility(airpol.getVisibility());
				}
				if (s0.contains("MaxTemp")) {
					AirPol.setMaxTemp(airpol.getMaxtemp());
				}
				if (s0.contains("MinTemp")) {
					AirPol.setMinTemp(airpol.getMintemp());
				}
				if (s0.contains("DryBulbTemp")) {
					AirPol.setDryBulbTemp(airpol.getDrybulbtemp());
				}
				resultList.add(AirPol);
			}
		}
		return resultList;
	}

	@Override
	public Date dailyInitTime() {
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "observtime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "airpol_live_qx");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_live_qx>>() {
			}).get(0).getObservtime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public Date liveInitTime() {
		// TODO Auto-generated method stub
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "observtime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "msgmediumsmallscale");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message =  procon.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<airpol_live_qx>>() {
			}).get(0).getObservtime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public List<String> initStations() {
		List<smallscale_station> listData = new ArrayList<smallscale_station>();
		List<String> resultList = new ArrayList<>();
		Map<String, String> selectParam = new HashMap<String, String>();
		com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect selectRequest = new com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("stationname");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setParam("stationid like '5%' and county like '"+DbConfig.COUNTY+"%'");
		try {
			selectParam.put("method", "smallscale_station");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = procon.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<smallscale_station>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		for (smallscale_station listDatum : listData) {
			resultList.add(listDatum.getStationname());
		}
		return resultList;
	}

}
